<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Shop_Product_Model extends MY_Model {
    private $userId = 0;

    public function __construct() {
        parent::__construct();
        $this->load->model('entity/product_details_entity', 'Product_Details_Entity');
        $this->userId = $this->session->userdata('user_id');
    }

    public function countAllProduct($option = array(), $need_login=true) {
        $this->db->select('a.id', true);
        $this->db->from($this->table_product_name .         ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name            . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name        . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name            . ' e', 'c.user_id=e.id');
        $this->db->join($this->table_shop_category_name   . ' f', 'a.shop_cate_id=f.id');
        $this->db->where('a.deleted_at', NULL);
        $this->db->where('b.is_avatar', ON);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        if($need_login){
            $this->db->where('e.id', $this->userId);
        }
        $this->db->where('f.deleted_at', null);
        if (count($option) > 0) {
            $this->db->where($option);
        }
        return $this->db->count_all_results();
    }

    public function paging($limit, $offset, $option = array(), $select=array(), $need_login=true) {
        if(count($select)>0){
            $select = implode(',', $select);
        } else {
            $select = "a.id AS product_id, a.title AS product_title, a.slug AS product_slug,
                        a.cate_id AS category_id, a.shop_id AS shop_id, a.shop_cate_id,
                        a.created_at AS product_created_at, a.active_flg AS product_active_flg, a.sale_flg AS product_sale_flg,
                        a.deleted_at AS product_deleted_at,
                    b.id as product_detail_id, b.code, b.image, b.price_new, b.price_old,b.is_avatar,
                    c.title AS shop_title,c.slug AS shop_slug,
                    d.title AS category_title, d.slug AS category_slug,
                    e.image_folder AS shop_image_folder,
                    f.title AS shop_cate_title, f.slug AS shop_cate_slug";
        }
        $result = null;
        $this->db->select($select, true);
        $this->db->from($this->table_product_name .         ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name            . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name        . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name            . ' e', 'c.user_id=e.id');
        $this->db->join($this->table_shop_category_name   . ' f', 'a.shop_cate_id=f.id');
        $this->db->where('a.deleted_at', NULL);
        $this->db->where('b.is_avatar', ON);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        if($need_login){
            $this->db->where('e.id', $this->userId);
        }
        $this->db->where('f.deleted_at', null);
        if (count($option) > 0) {
            $this->db->where($option);
        }
        $this->db->order_by('a.created_at', 'desc');
        $this->db->order_by('a.id', 'desc');
        $this->db->limit($limit, $offset);
        $result = $this->db->get()->result('Product_Details_Entity');

        return $result;
    }

    public function splitProductByCategory($option = array(), $select=array(), $need_login=true) {
        if(count($select)>0){
            $select = implode(',', $select);
        } else {
            $select = "a.id AS product_id, a.title AS product_title, a.slug AS product_slug,
                        a.cate_id AS category_id, a.shop_id AS shop_id, a.shop_cate_id,
                        a.created_at AS product_created_at, a.active_flg AS product_active_flg,
                        a.deleted_at AS product_deleted_at,
                    b.id as product_detail_id, b.code, b.image, b.price_new, b.price_old,b.is_avatar,
                    c.title AS shop_title,c.slug AS shop_slug,
                    d.title AS category_title, d.slug AS category_slug,
                    e.image_folder AS shop_image_folder,
                    f.title AS shop_cate_title, f.slug AS shop_cate_slug";
        }
        $result = null;
        $this->db->select($select, true);
        $this->db->from($this->table_product_name .         ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name            . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name        . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name            . ' e', 'c.user_id=e.id');
        $this->db->join($this->table_shop_category_name   . ' f', 'a.shop_cate_id=f.id');
        $this->db->where('a.deleted_at', NULL);
        $this->db->where('b.is_avatar', ON);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        if($need_login){
            $this->db->where('e.id', $this->userId);
        }
        $this->db->where('f.deleted_at', null);
        if (count($option) > 0) {
            $this->db->where($option);
        }
        $this->db->order_by('a.created_at', 'desc');
        $this->db->order_by('a.id', 'desc');
        $result = $this->db->get()->result('Product_Details_Entity');

        return $result;
    }

    public function countSearch($key) {
        $select = "a.id";
        $this->db->select($select, true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'c.user_id=e.id');
        $this->db->join($this->table_shop_category_name . ' f', 'a.shop_cate_id=f.id');
        $this->db->where('b.is_avatar', 1);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        $this->db->where('e.id', $this->userId);
        $this->db->where('f.deleted_at', null);
        $likeq = "a.title LIKE '%{$key}%'";
        $likeq .= " OR a.slug LIKE '%{$key}%'";
        $likeq .= " OR b.code LIKE '%{$key}%'";
        $likeq .= " OR b.price_old LIKE '%{$key}%'";
        $likeq .= " OR b.price_new LIKE '%{$key}%'";
        $likeq .= " OR b.description LIKE '%{$key}%'";
        $likeq .= " OR c.title LIKE '%{$key}%'";
        $likeq .= " OR c.slug LIKE '%{$key}%'";
        $likeq .= " OR d.title LIKE '%{$key}%'";
        $likeq .= " OR d.slug LIKE '%{$key}%'";
        $likeq .= " OR f.title LIKE '%{$key}%'";
        $likeq .= " OR f.slug LIKE '%{$key}%'";
        $likeq = "(" . $likeq . ")";
        $this->db->where($likeq);
        return $this->db->count_all_results();
    }

    public function search($limit, $offset, $key) {
        $select = "a.id AS product_id, a.title AS product_title, a.slug AS product_slug,
                        a.cate_id AS category_id, a.shop_id AS shop_id, a.shop_cate_id,
                        a.created_at AS product_created_at, a.active_flg AS product_active_flg,
                        a.deleted_at AS product_deleted_at,
                    b.code, b.image, b.price_new, b.price_old,b.id as product_detail_id,
                    c.title AS shop_title,c.slug AS shop_slug, e.image_folder AS shop_image_folder,
                    d.title AS category_title, d.slug AS category_slug,
                    f.title AS shop_cate_title, f.slug AS shop_cate_slug";
        $this->load->model('entity/product_details_entity', 'Product_Details_Entity');
        $this->db->select($select, true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'c.user_id=e.id');
        $this->db->join($this->table_shop_category_name . ' f', 'a.shop_cate_id=f.id');
        $this->db->where('b.is_avatar', 1);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        $this->db->where('e.id', $this->userId);
        $this->db->where('f.deleted_at', null);
        $likeq = "a.title LIKE '%{$key}%'";
        $likeq .= " OR a.slug LIKE '%{$key}%'";
        $likeq .= " OR b.code LIKE '%{$key}%'";
        $likeq .= " OR b.price_old LIKE '%{$key}%'";
        $likeq .= " OR b.price_new LIKE '%{$key}%'";
        $likeq .= " OR b.description LIKE '%{$key}%'";
        $likeq .= " OR c.title LIKE '%{$key}%'";
        $likeq .= " OR c.slug LIKE '%{$key}%'";
        $likeq .= " OR d.title LIKE '%{$key}%'";
        $likeq .= " OR d.slug LIKE '%{$key}%'";
        $likeq .= " OR f.title LIKE '%{$key}%'";
        $likeq .= " OR f.slug LIKE '%{$key}%'";
        $likeq = "(" . $likeq . ")";
        $this->db->where($likeq);
        $this->db->order_by('a.id');
        $this->db->limit($limit, $offset);
        $result = $this->db->get()->result('Product_Details_Entity');
        return $result;
    }

    public function get($slug, $option = array(), $select=array(), $need_login = true) {
        $result = null;
        if(count($select)>0){
            $select = implode(',', $select);
        } else {
            $select = "a.id, a.title, a.slug, a.shop_cate_id, a.sale_flg, a.active_flg, a.deleted_at, c.slug AS shop_slug, e.image_folder AS shop_image_folder";
        }
        $this->db->select($select, true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'c.user_id=e.id');
        $this->db->join($this->table_shop_category_name . ' f', 'a.shop_cate_id=f.id');
        $this->db->where('a.slug', $slug);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        if($need_login){
            $this->db->where('e.id', $this->userId);
        }
        $this->db->where('f.deleted_at', null);
        if (count($option) > 0) {
            $this->db->where($option);
        }
        $this->load->model('entity/product_entity', 'Product_Entity');
        $result = $this->db->get()->first_row('Product_Entity');
        return $result;
    }

    public function getProductInSet($product_id) {
        $result = null;
        $select = "b.id, b.code, b.image, b.price_new, b.price_old, b.description, b.is_avatar, b.deleted_at";
        $this->db->select($select, true);
        $this->db->from($this->table_product_details_name . ' b');
        $this->db->where('b.product_id', $product_id);
        $result = $this->db->get()->result('Product_Details_Entity');
        return $result;
    }

    public function getCurrentShop() {
        $result = null;
        $this->load->model('entity/shop_entity', 'Shop_Entity');
        $select = "c.id, c.title, c.slug, e.image_folder";
        $this->db->select($select, true);
        $this->db->from($this->table_shop_name . ' c');
        $this->db->join($this->table_user_name . ' e', 'c.user_id=e.id');
        $this->db->where('e.id', $this->userId);
        $result = $this->db->get()->first_row('Shop_Entity');
        return $result;
    }

    public function checkLimitUploadProduct($shop_id) {
        //update count product for management of packet
        $this->db->select('a.id, a.upload_time, b.product_count');
        $this->db->from($this->table_shop_packet_name . ' as a');
        $this->db->join($this->table_packet_name . ' b', 'a.packet_id=b.id');
        $this->db->where(array('a.shop_id' => $shop_id, 'a.deleted_at'=>NULL));
        $this->db->where('b.deleted_at', NULL);
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.upload_time < b.product_count');
        $result = $this->db->get()->result();
        return $result;
    }

    public function insert($data) {
        $limit = $this->checkLimitUploadProduct($data[$this->table_product_name]['shop_id']);
        if(count($limit)<1){
            return false;
        }
        $this->db->trans_begin();
        $this->db->insert($this->table_product_name, $data['product']);
        $iId = $this->db->insert_id();
        if (isset($data['product_detail_new'])) {
            foreach ($data['product_detail_new'] AS $p) {
                $p['product_id'] = $iId;
                $this->db->insert($this->table_product_details_name, $p);
            }
        }
        $oLimit=$limit[0];
        $this->db->where('id', $oLimit->id);
        $this->db->update($this->table_shop_packet_name, array('upload_time'=>$oLimit->upload_time + 1));

        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    public function update($id, $data) {
        $this->db->trans_begin();
        $this->db->where('id', $id);
        $this->db->update($this->table_product_name, $data['product']);
        if (isset($data['product_detail_update'])) {
            foreach ($data['product_detail_update'] AS $puid => $p) {
                $this->db->where('id', $puid);
                $this->db->update($this->table_product_details_name, $p);
            }
        }
        if (isset($data['product_detail_new'])) {
            foreach ($data['product_detail_new'] AS $p) {
                $this->db->insert($this->table_product_details_name, $p);
            }
        }
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    public function flash_update($id, $data) {
        $this->db->trans_begin();
        $this->db->where('id', $id);
        $this->db->update($this->table_product_name, $data);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    public function checkProductExist($option, $id=null) {
        $this->db->select("a.id", true);
        $this->db->from($this->table_product_name . ' AS a');
        if(isset($option['shop_id'])){
            $this->db->where('a.shop_id', $option['shop_id']);
        }
        $strOrWhere = "(a.title='{$option['title']}' OR a.slug='{$option['slug']}')";
        $this->db->where($strOrWhere);
        if($id!=null){
            $this->db->where('a.id <>', $id);
        }
        return $this->db->count_all_results();
    }

    public function checkProductCodeExist($option, $id=null) {
        $this->db->select("a.id", true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        if(isset($option['shop_id'])){
            $this->db->where('a.shop_id', $option['shop_id']);
        }
        $this->db->where('b.code', $option['code']);
        if($id!=null){
            $this->db->where('b.id <>', $id);
        }
        return $this->db->count_all_results();
    }

    public function getShopCategoryProducts($shop_id, $shop_cate) {
        $result = null;
        $this->load->model('entity/product_details_entity', 'Product_Details_Entity');
        $this->db->select('
            a.id as product_id, a.title, a.cate_id, a.shop_id, a.slug, a.sale_flg,
            b.id, b.id as product_detail_id, b.code, b.image, b.price_new, b.price_old, b.description,
            c.slug AS shop_slug, f.image_folder AS shop_image_folder', true
        );
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_shop_category_name . ' e', 'e.shop_id=c.id and a.shop_cate_id=e.id');
        $this->db->join($this->table_user_name . ' f', 'c.user_id=f.id');
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('f.deleted_at', null);
        $this->db->where('a.active_flg', 1);
        $this->db->where('b.is_avatar', 1);
        $this->db->where('e.slug', $shop_cate);
        $this->db->where('a.shop_id', $shop_id);
        $this->db->where('e.shop_id', $shop_id);
        $this->db->order_by('a.updated_at', 'asc');
        $this->db->order_by('a.created_at', 'asc');
        $result = $this->db->get()->result('Product_Details_Entity');
        return $result;
    }

    public function countShopProduct($shopId, $shop_slug) {
        $this->db->select('distinct(a.id)');
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_shop_category_name . ' e', 'e.cate_id=d.id and e.shop_id=c.id');
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.is_avatar', 1);
        $this->db->where('b.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('a.shop_id', $shopId);
        return $this->db->count_all_results();;
    }

    public function getShopProducts($shopId, $isTop = false, $limit=null, $offset=null) {
        $result = null;
        $this->load->model('entity/product_details_entity', 'Product_Details_Entity');
        $this->db->select('distinct(a.id), a.title, a.cate_id, a.shop_id, b.code, a.slug, b.image,
                b.price_new, b.price_old, f.image_folder AS shop_image_folder', true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_shop_category_name . ' e', 'e.cate_id=d.id and e.shop_id=c.id');
        $this->db->join($this->table_user_name . ' f', 'c.user_id=f.id');
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.is_avatar', 1);
        $this->db->where('b.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('f.deleted_at', null);
        $this->db->where('a.shop_id', $shopId);
        $this->db->order_by('a.created_at', 'desc');
        $this->db->order_by('a.updated_at', 'desc');
        $this->db->order_by('a.id', 'desc');
        if ($isTop) {
            $this->db->limit(PRODUCT_LIMIT);
        }
        if($limit!=null){
            $this->db->limit($limit, $offset);
        }

        $result = $this->db->get()->result('Product_Details_Entity');
        return $result;
    }

    public function countFrontSearchProduct($shop_id, $key) {
        $aWhere = array(
            'a.shop_id' => $shop_id,
            'a.deleted_at' => null,
            'b.deleted_at' => null,
            'c.deleted_at' => null,
            'd.deleted_at' => null,
            'a.active_flg' => 1,
            'b.is_avatar' => 1);
        $this->load->model('entity/product_details_entity', 'Product_Details_Entity');
        $this->db->select('a.id');
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_category_name . ' c', 'a.cate_id=c.id');
        $this->db->join($this->table_shop_name . ' d', 'a.shop_id=d.id');
        $this->db->where($aWhere);
        $likeq = "a.title LIKE '%{$key}%'";
        $likeq .= " OR a.slug LIKE '%{$key}%'";
        $likeq .= " OR b.code LIKE '%{$key}%'";
        $likeq .= " OR b.price_old LIKE '%{$key}%'";
        $likeq .= " OR b.price_new LIKE '%{$key}%'";
        $likeq .= " OR b.image LIKE '%{$key}%'";
        $likeq .= " OR b.description LIKE '%{$key}%'";
        $likeq .= " OR c.title LIKE '%{$key}%'";
        $likeq .= " OR c.slug LIKE '%{$key}%'";
        $likeq .= " OR d.title LIKE '%{$key}%'";
        $likeq .= " OR d.slug LIKE '%{$key}%'";
        $likeq = "(" . $likeq . ")";
        $this->db->where($likeq);
        return $this->db->count_all_results();
    }

    public function searchFrontProduct($shop_id, $key, $limit, $offset) {
        $aWhere = array(
            'a.shop_id' => $shop_id,
            'a.deleted_at' => null,
            'b.deleted_at' => null,
            'c.deleted_at' => null,
            'd.deleted_at' => null,
            'a.active_flg' => 1,
            'b.is_avatar' => 1);
        $this->load->model('entity/product_details_entity', 'Product_Details_Entity');
        $this->db->select('distinct(a.id) as product_id, a.title, a.cate_id, a.shop_id, a.slug, a.highlight_flg, a.sale_flg,
            b.id, b.id as product_detail_id, b.code, b.image, b.price_new, b.price_old, b.description,
            c.title as category_title, c.slug as category_slug,
            d.title as shop_title, d.slug as shop_slug, e.image_folder AS shop_image_folder', true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_category_name . ' c', 'a.cate_id=c.id');
        $this->db->join($this->table_shop_name . ' d', 'a.shop_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'd.user_id=e.id');
        $this->db->where($aWhere);
        $likeq = "a.title LIKE '%{$key}%'";
        $likeq .= " OR a.slug LIKE '%{$key}%'";
        $likeq .= " OR b.code LIKE '%{$key}%'";
        $likeq .= " OR b.price_old LIKE '%{$key}%'";
        $likeq .= " OR b.price_new LIKE '%{$key}%'";
        $likeq .= " OR b.image LIKE '%{$key}%'";
        $likeq .= " OR b.description LIKE '%{$key}%'";
        $likeq .= " OR c.title LIKE '%{$key}%'";
        $likeq .= " OR c.slug LIKE '%{$key}%'";
        $likeq .= " OR d.title LIKE '%{$key}%'";
        $likeq .= " OR d.slug LIKE '%{$key}%'";
        $likeq = "(" . $likeq . ")";
        $this->db->where($likeq);
        $this->db->limit($limit, $offset);
        $this->db->order_by('a.created_at', 'desc');
        $this->db->order_by('a.updated_at', 'desc');
        $this->db->order_by('a.id', 'desc');
        $result = $this->db->get()->result('Product_Details_Entity');
        return $result;
    }

    //////////////// for shop template ////////////////////
    public function getTopBuyProducts($shopId, $limit=0) {
        $select = "a.id, a.title, a.slug,
                    b.id as product_detail_id, b.code, b.image, b.price_new, b.price_old,
                    c.slug AS shop_slug, e.image_folder AS shop_image_folder";
        $result = null;
        $this->db->select($select, true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'c.user_id=e.id');
        $this->db->join($this->table_shop_category_name . ' f', 'a.shop_cate_id=f.id');
        $this->db->join($this->table_order_detail_name . ' g', 'g.product_id=a.id');
        $this->db->join($this->table_order_name . ' h', 'g.order_id=h.id');
        $this->db->where('a.shop_id', $shopId);
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.deleted_at', null);
        $this->db->where('b.is_avatar', 1);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        $this->db->where('f.deleted_at', null);
        $this->db->where('h.deleted_at', null);
        $this->db->order_by('g.count', 'desc');
        $this->db->order_by('a.created_at', 'desc');
        $this->db->order_by('a.updated_at', 'desc');
        $this->db->order_by('a.id', 'desc');
        $this->db->limit($limit, 0);
        $result = $this->db->get()->result('Product_Details_Entity');
        return $result;
    }

    public function getNewProducts($shopId, $limit=0) {
        $select = "a.id, a.title, a.slug,
                    b.id as product_detail_id, b.code, b.image, b.price_new, b.price_old,
                    c.slug AS shop_slug, e.image_folder AS shop_image_folder";
        $result = null;
        $this->db->select($select, true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'c.user_id=e.id');
        $this->db->join($this->table_shop_category_name . ' f', 'a.shop_cate_id=f.id');
        $this->db->where('a.shop_id', $shopId);
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.deleted_at', null);
        $this->db->where('b.is_avatar', 1);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        $this->db->where('f.deleted_at', null);
        $this->db->order_by('a.created_at', 'desc');
        $this->db->order_by('a.updated_at', 'desc');
        $this->db->order_by('a.id', 'desc');
        $this->db->limit($limit, 0);
        $result = $this->db->get()->result('Product_Details_Entity');
        return $result;
    }

    public function getSaleProducts($shopId, $limit=0) {
        $select = "a.id, a.title, a.slug, a.sale_flg,
                    b.id as product_detail_id, b.code, b.image, b.price_new, b.price_old,
                    c.slug AS shop_slug, e.image_folder AS shop_image_folder";
        $result = null;
        $this->db->select($select, true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'c.user_id=e.id');
        $this->db->join($this->table_shop_category_name . ' f', 'a.shop_cate_id=f.id');
        $this->db->where('a.shop_id', $shopId);
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.deleted_at', null);
        $this->db->where('b.is_avatar', 1);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        $this->db->where('f.deleted_at', null);
        $this->db->where('a.sale_flg', ON);
        $this->db->order_by('a.created_at', 'desc');
        $this->db->order_by('a.updated_at', 'desc');
        $this->db->order_by('a.id', 'desc');
        $this->db->limit($limit, 0);
        $result = $this->db->get()->result('Product_Details_Entity');
        return $result;
    }

    public function getHighlightProducts($shopId, $limit=0) {
        $select = "a.id, a.title, a.slug,
                    b.id as product_detail_id, b.code, b.image, b.price_new, b.price_old,
                    c.slug AS shop_slug, e.image_folder AS shop_image_folder";
        $result = null;
        $this->db->select($select, true);
        $this->db->select('SUBSTRING(b.description, 1, 50) as description', FALSE);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'c.user_id=e.id');
        $this->db->join($this->table_shop_category_name . ' f', 'a.shop_cate_id=f.id');
        $this->db->where('a.shop_id', $shopId);
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.highlight_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.deleted_at', null);
        $this->db->where('b.is_avatar', 1);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        $this->db->where('f.deleted_at', null);
        $this->db->order_by('a.created_at', 'desc');
        $this->db->order_by('a.updated_at', 'desc');
        $this->db->order_by('a.id', 'desc');
        $this->db->limit($limit, 0);
        $result = $this->db->get()->result('Product_Details_Entity');
        return $result;
    }
}
?>